package com.nuc.wxh.vaccine_appointment.vaccines.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuc.wxh.vaccine_appointment.base.exception.VaccineException;
import com.nuc.wxh.vaccine_appointment.base.result.ResultCodeEnum;
import com.nuc.wxh.vaccine_appointment.base.util.FormUtils;
import com.nuc.wxh.vaccine_appointment.base.util.JwtInfo;
import com.nuc.wxh.vaccine_appointment.base.util.JwtUtils;
import com.nuc.wxh.vaccine_appointment.base.util.MD5;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.SysUser;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.LoginVo;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.RegisterVo;
import com.nuc.wxh.vaccine_appointment.vaccines.mapper.SysUserMapper;
import com.nuc.wxh.vaccine_appointment.vaccines.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 前台系统用户信息 服务实现类
 * </p>
 *
 * @author 王鑫浩
 * @since 2021-03-10
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void register(RegisterVo registerVo) {
        String email = registerVo.getEmail();
        String mobile = registerVo.getMobile();
        String username = registerVo.getUsername();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        if (StringUtils.isEmpty(mobile) && StringUtils.isEmpty(email)) {
            throw new VaccineException(ResultCodeEnum.PARAM_ERROR);
        }
        //校验邮箱
        //注意: 这里需要使用 '|' 要两边都能检测到
       if (!StringUtils.isEmpty(email) | FormUtils.isEmail(email)) {
            //校验验证码
            String checkCode = redisTemplate.opsForValue().get(email);
            if (!code.equals(checkCode)) {
                throw new VaccineException(ResultCodeEnum.CODE_ERROR);
            }
        }
        //是否被注册的条件
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(mobile)){
            queryWrapper.eq("mobile",mobile);
        }
        if (!StringUtils.isEmpty(email)){
            queryWrapper.or().eq("email",email);
        }
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new VaccineException(ResultCodeEnum.REGISTER_USERINFO_ERROR);
        }

        //注册
        SysUser sysUser=new SysUser();
        sysUser.setUsername(username);
        sysUser.setMobile(mobile);
        sysUser.setEmail(email);
        sysUser.setPassword(MD5.encrypt(password));
        baseMapper.insert(sysUser);
    }

    @Override
    public void updatepwd(RegisterVo registerVo) {


        String email = registerVo.getEmail();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        if (!StringUtils.isEmpty(email) | FormUtils.isEmail(email)) {
            String checkCode = redisTemplate.opsForValue().get(email);
            if (!code.equals(checkCode)) {
                throw new VaccineException(ResultCodeEnum.CODE_ERROR);
            }
        }
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(email)){
            queryWrapper.or().eq("email",email);
        }
       SysUser sysUser = baseMapper.selectOne(queryWrapper);
        sysUser.setPassword(MD5.encrypt(password));
        baseMapper.updateById(sysUser);

    }

    @Override
    public String login(LoginVo loginVo) {
        String userInfo = loginVo.getUsername();
        String password = loginVo.getPassword();
        //使用邮箱或者手机号登录
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", userInfo).or().eq("email", userInfo);
        SysUser sysUser = baseMapper.selectOne(queryWrapper);

        if (sysUser == null) {
            throw new VaccineException(ResultCodeEnum.LOGIN_ERROR);
        }

        //校验密码
        if (!MD5.encrypt(password).equals(sysUser.getPassword())) {
            throw new VaccineException(ResultCodeEnum.LOGIN_ERROR);
        }

        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(sysUser.getId());
        jwtInfo.setRoles("普通用户");
        String jwtToken = JwtUtils.getJwtToken(jwtInfo, 12*60*60);

        return jwtToken;
    }

    @Override
    public Page<SysUser> selectPage(Page<SysUser> pageParam) {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        return baseMapper.selectPage(pageParam,wrapper);
    }
}
